<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'wms:jh:pick:jhPickDetailsPos:edit'"
    @register="registerDrawer"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <div>
      <BasicTable @register="registerTable" @row-click="handleRdrecordRowClick">
        <template #summary>
          <Table.Summary fixed>
            <Table.Summary.Row>
              <Table.Summary.Cell :index="0" :col-span="1" :style="{ textAlign: 'center' }"
                >合计</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="1" :col-span="2"></Table.Summary.Cell>
              <Table.Summary.Cell :index="3" :style="{ textAlign: 'center', }">
                {{ totalIqty.fqty }}</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="4" :style="{ textAlign: 'center', }">
                {{ totalIqty.sumQty }}</Table.Summary.Cell
              >
            </Table.Summary.Row>
          </Table.Summary>
        </template>
      </BasicTable>
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsRdRd32Rdrecord32TzList',
  });
</script>
<script lang="ts" setup>
  import { unref, defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { companyTreeData } from '/@/api/sys/company';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { whDownTaskDetailsPosListData } from '/@/api/wms/wh/down/whDownTaskMain';
  import { Table } from 'ant-design-vue';
  const { t } = useI18n('rd.rd32.rdrecord32');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const emit = defineEmits(['success', 'register']);
  const hid = ref<string>('');

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('拣货单-明细附表-货位表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('货位编码'),
        field: 'posCode',
        component: 'Input',
      },
      {
        label: t('货位名称'),
        field: 'posName',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'jhStatus',
        component: 'Select',
        componentProps: {
          dictType: 'jh_pos_status',
          allowClear: true,
        },
      },
    ],
  };
  const tableColumns: BasicColumn[] = [
  {
      title: t('货位编码'),
      dataIndex: 'posCode',
      key: 'a.pos_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('货位名称'),
      dataIndex: 'posName',
      key: 'a.pos_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('累计下架数'),
      dataIndex: 'sumPickQty',
      key: 'a.sum_pick_qty',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'jhStatus',
      key: 'a.jh_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'jh_main_status',
    },
  ];

  // const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    hid.value = data.id;
    setProps({
      beforeFetch: (params) => {
        params.hid = data.id;
        return params;
      },
    });

    reload();
    setDrawerProps({ loading: false });
    console.log('data', data);
  });
  const [registerTable, { reload, getForm, setProps, getDataSource }] = useTable({
    api: whDownTaskDetailsPosListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    // showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleRdrecordRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  const totalIqty = computed(() => {
    const tableData = getDataSource();
    let sumFqty = tableData.reduce((total, item) => total + (item.iqty || 0), 0);
    let sumQty = tableData.reduce((total, item) => total + (item.sumPickQty || 0), 0);
    return {
      fqty: sumFqty,
      sumQty: sumQty,
    };
  });
</script>
<style scoped>
  .ant-input{
    border: 1px solid red !important;
  }
  .fontWeight-bold{
    font-weight: bold;
  }
</style>
